<?php
/**
 * Created by PhpStorm.
 * User: YUWENXUAN
 * Date: 2017/4/10
 * Time: 16:38
 */

abstract class FileDir{
    protected $name;
    protected $basename;
    protected $type;
    protected $size;
    protected $ctime;   //创建时间
    protected $mtime;   //修改时间
    protected $atime;   //最后访问时间
    protected $able;    //文件权限


    function __construct($filename)
    {
        $this ->basename = basename($filename);
//        $this ->ctime = ctime($filename,'c');
        $this->ctime = $this->getDateTime($filename,'c');
        $this->mtime = $this->getDateTime($filename,'m');
        $this->atime = $this->getDateTime($filename,'a');
        $this->able  = $this->fileAble($filename);
    }

    /**
     * @return mixed
     */
    public function getName()
    {
        return $this->name;
    }

    /**
     * @return string
     */
    public function getBasename(): string
    {
        return $this->basename;
    }

    /**
     * @return mixed
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * @return mixed
     */
    public function getSize()
    {
        return $this->size;
    }

    /**
     * @return mixed
     */
    public function getCtime()
    {
        return $this->ctime;
    }

    /**
     * @return mixed
     */
    public function getMtime()
    {
        return $this->mtime;
    }

    /**
     * @return mixed
     */
    public function getAtime()
    {
        return $this->atime;
    }

    /**
     * @return mixed
     */
    public function getAble()
    {
        return $this->able;
    }

    abstract protected  function delFile();
    abstract protected  function copyFile($dFile);

    public function moveFile($newName){
        if (rename($this->name,$newName)){
            $this->name = $newName;
            return true;
        }else{
            return false;
        }
    }
    public function toSize($bytes){
        if ($bytes >= pow(2,40)){   //pow 幂次方 2的40次方
            $return = round($bytes / pow(1024,4),2);
            //将字节大小除以1024的4次方
            //round() 函数对浮点数进行四舍五入。round(x,prec) prec 可选，舍入的位数
            $suffix = "TB";

        }elseif ($bytes >= pow(2,30)){
            $return = round($bytes / pow(1024,3),2);
            $suffix = "GB";
        }elseif ($bytes >= pow(2,20)){
            $return = round($bytes / pow( 1024,2),2);
            $suffix = "MB";
        }elseif (($bytes >= pow(2,10))){
            $return = round($bytes / pow(1024, 1),2);
            $suffix = "KB";
        }else{
            $return = $bytes;
            $suffix = "Byte";
        }
        return $return." " . $suffix;
    }
    public  function getDateTime($filename,$cate = 'm'){
        date_default_timezone_set("Etc/GMT-8"); //设置不同时区
        switch ($cate){
            case 'm':
                return date("Y-m-y H:i:s",fileatime($filename));
                break;
            case 'c':
                return date("Y-m-y H:i:s",fileatime($filename));
                break;
            case 'a':
                return date("Y-m-y H:i:s",fileatime($filename));
                break;

            default:
                return "0000-00-00 00:00:00";
        }
    }


    //返回值   权限值 4 可读\ 2 可写 \ 1 可执行   7 为4+2+1  表示可读可写可执行 0没有权限
    protected  function fileAble(){
        $read = 0;    // 不可读
        $write = 0;    // 可写
        $exe = 0;     //执行
        if (is_readable($this->name)){
            $read = 4;
        }
        if (is_writable($this->name)){
            $write = 2;
        }
        if (is_executable($this->name)){
            $exe =1;
        }
        return $read + $write +$exe;
    }


    function __toString()
    {
        // TODO: Implement __toString() method.

        $fileContent = "";
        $fileContent = "文件名称：".$this->getName()."<br>";
        $fileContent = "文件类型".$this->getType()."<br>";
        $fileContent = "大小".$this->getSize()."<br>";
        $fileContent = "权限". $this->getAble()."<br>";
        $fileContent = "创建时间".$this->getCtime()."<br>";
        $fileContent = "修改时间".$this->getMtime()."<br>";
        $fileContent = "访问时间".$this->getAtime()."<br>";
        return $fileContent;

    }
}